<?php 
class ControllerPaymentOgone extends Controller {
	private $error = array(); 

	public function index() {
		$this->load->language('payment/ogone');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('ogone', $this->request->post);				
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token']);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');
		$this->data['text_sim'] = $this->language->get('text_sim');
		$this->data['text_test'] = $this->language->get('text_test');
		$this->data['text_live'] = $this->language->get('text_live');
		$this->data['text_payment'] = $this->language->get('text_payment');
		$this->data['text_authorization'] = $this->language->get('text_authorization');
		$this->data['text_sha1'] = $this->language->get('text_sha1');
		$this->data['text_sha256'] = $this->language->get('text_sha256');
		$this->data['text_sha512'] = $this->language->get('text_sha512');
		
		$this->data['entry_pspid'] = $this->language->get('entry_pspid');
		$this->data['entry_mode'] = $this->language->get('entry_mode');
		$this->data['entry_transaction'] = $this->language->get('entry_transaction');
		$this->data['entry_total'] = $this->language->get('entry_total');	
		$this->data['entry_status'] = $this->language->get('entry_status');		
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['entry_captured_status'] = $this->language->get('entry_captured_status');
		$this->data['entry_authorized_status'] = $this->language->get('entry_authorized_status');
		$this->data['entry_authorization_waiting_status'] = $this->language->get('entry_authorization_waiting_status');
		$this->data['entry_payment_processing_status'] = $this->language->get('entry_payment_processing_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_bgcolor'] = $this->language->get('entry_bgcolor');
		$this->data['entry_txtcolor'] = $this->language->get('entry_txtcolor');
		$this->data['entry_tblbgcolor'] = $this->language->get('entry_tblbgcolor');
		$this->data['entry_tbltxtcolor'] = $this->language->get('entry_tbltxtcolor');
		$this->data['entry_buttonbgcolor'] = $this->language->get('entry_buttonbgcolor');
		$this->data['entry_buttontxtcolor'] = $this->language->get('entry_buttontxtcolor');
		$this->data['entry_fonttype'] = $this->language->get('entry_fonttype');
		$this->data['entry_logo'] = $this->language->get('entry_logo');
		$this->data['entry_sha_method'] = $this->language->get('entry_sha_method');
		$this->data['entry_passphrase_in'] = $this->language->get('entry_passphrase_in');
		$this->data['entry_passphrase_out']= $this->language->get('entry_passphrase_out');
		$this->data['entry_log'] = $this->language->get('entry_log');
				
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['tab_general'] = $this->language->get('tab_general');
		$this->data['tab_design'] = $this->language->get('tab_design');
		$this->data['tab_security'] = $this->language->get('tab_security');
		

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['passphrase_in'])) {
			$this->data['error_passphrase_in'] = $this->error['passphrase_in'];
		} else {
			$this->data['error_passphrase_in'] = '';
		}

 		if (isset($this->error['passphrase_out'])) {
			$this->data['error_passphrase_out'] = $this->error['passphrase_out'];
		} else {
			$this->data['error_passphrase_out'] = '';
		}

 		if (isset($this->error['pspid'])) {
			$this->data['error_pspid'] = $this->error['pspid'];
		} else {
			$this->data['error_pspid'] = '';
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_payment'),
			'href'      => HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'],
      		'separator' => ' :: '
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => HTTPS_SERVER . 'index.php?route=payment/ogone&token=' . $this->session->data['token'],
      		'separator' => ' :: '
   		);
				
		$this->data['action'] = HTTPS_SERVER . 'index.php?route=payment/ogone&token=' . $this->session->data['token'];
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'];
		
		if (isset($this->request->post['ogone_pspid'])) {
			$this->data['ogone_pspid'] = $this->request->post['ogone_pspid'];
		} else {
			$this->data['ogone_pspid'] = $this->config->get('ogone_pspid');
		}
		
		if (isset($this->request->post['ogone_password'])) {
			$this->data['ogone_password'] = $this->request->post['ogone_password'];
		} else {
			$this->data['ogone_password'] = $this->config->get('ogone_password');
		}

		if (isset($this->request->post['ogone_mode'])) {
			$this->data['ogone_mode'] = $this->request->post['ogone_mode'];
		} else {
			$this->data['ogone_mode'] = $this->config->get('ogone_mode');
		}
		
		if (isset($this->request->post['ogone_transaction'])) {
			$this->data['ogone_transaction'] = $this->request->post['ogone_transaction'];
		} else {
			$this->data['ogone_transaction'] = $this->config->get('ogone_transaction');
		}
		
		if (isset($this->request->post['ogone_total'])) {
			$this->data['ogone_total'] = $this->request->post['ogone_total'];
		} else {
			$this->data['ogone_total'] = $this->config->get('ogone_total'); 
		} 
				
		if (isset($this->request->post['ogone_authorized_status_id'])) {
			$this->data['ogone_authorized_status_id'] = $this->request->post['ogone_authorized_status_id'];
		} else {
			$this->data['ogone_authorized_status_id'] = $this->config->get('ogone_authorized_status_id'); 
		}
		 
		if (isset($this->request->post['ogone_payment_processing_status_id'])) {
			$this->data['ogone_payment_processing_status_id'] = $this->request->post['ogone_payment_processing_status_id'];
		} else {
			$this->data['ogone_payment_processing_status_id'] = $this->config->get('ogone_payment_processing_status_id'); 
		} 

		if (isset($this->request->post['ogone_captured_status_id'])) {
			$this->data['ogone_captured_status_id'] = $this->request->post['ogone_captured_status_id'];
		} else {
			$this->data['ogone_captured_status_id'] = $this->config->get('ogone_captured_status_id'); 
		} 

		if (isset($this->request->post['ogone_authorization_waiting_status_id'])) {
			$this->data['ogone_authorization_waiting_status_id'] = $this->request->post['ogone_authorization_waiting_status_id'];
		} else {
			$this->data['ogone_authorization_waiting_status_id'] = $this->config->get('ogone_authorization_waiting_status_id'); 
		} 

		$this->load->model('localisation/order_status');
		
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
		
		if (isset($this->request->post['ogone_geo_zone_id'])) {
			$this->data['ogone_geo_zone_id'] = $this->request->post['ogone_geo_zone_id'];
		} else {
			$this->data['ogone_geo_zone_id'] = $this->config->get('ogone_geo_zone_id'); 
		} 
		
		$this->load->model('localisation/geo_zone');
										
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		
		if (isset($this->request->post['ogone_status'])) {
			$this->data['ogone_status'] = $this->request->post['ogone_status'];
		} else {
			$this->data['ogone_status'] = $this->config->get('ogone_status');
		}
		
		if (isset($this->request->post['ogone_log'])) {
			$this->data['ogone_log'] = $this->request->post['ogone_log'];
		} else {
			$this->data['ogone_log'] = $this->config->get('ogone_log');
		}
		
		if (isset($this->request->post['ogone_sort_order'])) {
			$this->data['ogone_sort_order'] = $this->request->post['ogone_sort_order'];
		} else {
			$this->data['ogone_sort_order'] = $this->config->get('ogone_sort_order');
		}
		
		if (isset($this->request->post['ogone_title'])) {
			$this->data['ogone_title'] = $this->request->post['ogone_title'];
		} else {
			$this->data['ogone_title'] = $this->config->get('ogone_title');
		}
		
		if (isset($this->request->post['ogone_bgcolor'])) {
			$this->data['ogone_bgcolor'] = $this->request->post['ogone_bgcolor'];
		} else {
			$this->data['ogone_bgcolor'] = $this->config->get('ogone_bgcolor');
		}
		
		if (isset($this->request->post['ogone_txtcolor'])) {
			$this->data['ogone_txtcolor'] = $this->request->post['ogone_txtcolor'];
		} else {
			$this->data['ogone_txtcolor'] = $this->config->get('ogone_txtcolor');
		}
		
		if (isset($this->request->post['ogone_tblbgcolor'])) {
			$this->data['ogone_tblbgcolor'] = $this->request->post['ogone_tblbgcolor'];
		} else {
			$this->data['ogone_tblbgcolor'] = $this->config->get('ogone_tblbgcolor');
		}
		
		if (isset($this->request->post['ogone_tbltxtcolor'])) {
			$this->data['ogone_tbltxtcolor'] = $this->request->post['ogone_tbltxtcolor'];
		} else {
			$this->data['ogone_tbltxtcolor'] = $this->config->get('ogone_tbltxtcolor');
		}
		
		if (isset($this->request->post['ogone_tbltxtcolor'])) {
			$this->data['ogone_tbltxtcolor'] = $this->request->post['ogone_tbltxtcolor'];
		} else {
			$this->data['ogone_tbltxtcolor'] = $this->config->get('ogone_tbltxtcolor');
		}
		
		if (isset($this->request->post['ogone_buttonbgcolor'])) {
			$this->data['ogone_buttonbgcolor'] = $this->request->post['ogone_buttonbgcolor'];
		} else {
			$this->data['ogone_buttonbgcolor'] = $this->config->get('ogone_buttonbgcolor');
		}
		
		if (isset($this->request->post['ogone_buttontxtcolor'])) {
			$this->data['ogone_buttontxtcolor'] = $this->request->post['ogone_buttontxtcolor'];
		} else {
			$this->data['ogone_buttontxtcolor'] = $this->config->get('ogone_buttontxtcolor');
		}
		
		if (isset($this->request->post['ogone_fonttype'])) {
			$this->data['ogone_fonttype'] = $this->request->post['ogone_fonttype'];
		} else {
			$this->data['ogone_fonttype'] = $this->config->get('ogone_fonttype');
		}
		
		if (isset($this->request->post['ogone_logo'])) {
			$this->data['ogone_logo'] = $this->request->post['ogone_logo'];
		} else {
			$this->data['ogone_logo'] = $this->config->get('ogone_logo');
		}
		
		if (isset($this->request->post['ogone_sha'])) {
			$this->data['ogone_sha'] = $this->request->post['ogone_sha'];
		} else {
			$this->data['ogone_sha'] = $this->config->get('ogone_sha');
		}
		
		if (isset($this->request->post['ogone_passphrase_in'])) {
			$this->data['ogone_passphrase_in'] = $this->request->post['ogone_passphrase_in'];
		} else {
			$this->data['ogone_passphrase_in'] = $this->config->get('ogone_passphrase_in');
		}
		
		if (isset($this->request->post['ogone_passphrase_out'])) {
			$this->data['ogone_passphrase_out'] = $this->request->post['ogone_passphrase_out'];
		} else {
			$this->data['ogone_passphrase_out'] = $this->config->get('ogone_passphrase_out');
		}
		
		$this->template = 'payment/ogone.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		// $this->response->setOutput($this->render());
	}

	private function validate() {		
		
		if (!$this->user->hasPermission('modify', 'payment/ogone')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (isset($this->request->post['ogone_pspid']) && strlen(trim($this->request->post['ogone_pspid'])) == 0) {
			$this->error['pspid'] = $this->language->get('error_pspid');
		}
		
		if (isset($this->request->post['ogone_passphrase_in']) && strlen(trim($this->request->post['ogone_passphrase_in'])) == 0) {
			$this->error['passphrase_in'] = $this->language->get('error_passphrase_in');
		}
		
		if (isset($this->request->post['ogone_passphrase_out']) && strlen(trim($this->request->post['ogone_passphrase_out'])) == 0) {
			$this->error['passphrase_out'] = $this->language->get('error_passphrase_out');
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
		
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
}
?>